package com.itheima.info.manger.system.dao;

import com.itheima.info.manger.system.domain.Student;

import java.util.ArrayList;

/*
学生操作数据类
使用集合来存储 学生对象

增
删
改
查询所有
根据学号查询
 */
public class OtherStudentDao implements BaseStudentDao {
    private static ArrayList<Student> students = new ArrayList<Student>();

    static {
        Student student1 = new Student("0001", "蔡徐坤", 27, "1994");
        Student student2 = new Student("0002", "黄子韬", 26, "1995");
        Student student3 = new Student("0003", "鹿晗", 28, "1993");
        Student student4 = new Student("0004", "吴亦凡", 30, "1991");
        students.add(student1);
        students.add(student2);
        students.add(student3);
        students.add(student4);
    }
    @Override
    public boolean addStudent(Student student) {
        students.add(student);
        return true;
    }
    @Override
    public void deleteStudent(String sid) {
        int index = getIndex(sid);
        students.remove(index);
    }

    public void updateStudent(String sid, Student newStu) {
        int index = getIndex(sid);
        students.set(index, newStu);
    }

    public Student[] findAllStudents() {
        //创建数组,将集合中的元素存入数组中
        Student[] studentArray = new Student[OtherStudentDao.students.size()];
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            studentArray[i]=student;

        }
        return studentArray;
    }

    //查索引的方法
    //参数 学号
    //返回值 int
    @Override
    public int getIndex(String sid) {
        for (int i = 0; i < students.size(); i++) {
            //students.get(i)
            Student stu = students.get(i);
            if (stu != null && stu.getSid().equals(sid)) {
                return i;
            }
        }
        return -1;
    }
}
